//
//  MenuTableViewCell.m
//  MKUI
//
//  Created by 苏尚进 on 2020/8/22.
//Copyright © 2020 moon. All rights reserved.
//

#import "MenuTableViewCell.h"

@interface MenuTableViewCell ()

@end

@implementation MenuTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectButton = [[MKUIFlowLayoutButton alloc] init];
        self.selectButton.imageSize = CGSizeMake(24,24);
        self.selectButton.contentInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [self.selectButton setImage:[UIImage imageNamed:@"icon_multiselection_not.png"] forState:UIControlStateNormal];
        [self.selectButton setImage:[UIImage imageNamed:@"icon_multiselection.png"] forState:UIControlStateSelected];
        [self.selectButton addTarget:self action:@selector(__onSelectButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.selectButton];
        //
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.iconImageView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            UIImageView *iconImageView = (UIImageView *)wrapper.originItem;
            CGSize s = [iconImageView mk_sizeThatFits:CGSizeMake(30, 60)];
            return s;
        }];
        [self.contentView addSubview:self.iconImageView];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleLabel];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.selectButton,w1,self.titleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsMake(10, 20, 10, 20) interitemSpacing:10];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	}
	return self;
}
- (void)setEditting:(BOOL)editting{
    self.selectButton.hidden = !editting;
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
- (void)customLayoutSubviews{
	[super customLayoutSubviews];
	CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (void)__onSelectButtonDidTap:(id)sender{
    [self.cellModel setSelected:!self.cellModel.selected animated:NO];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    self.selectButton.selected = selected;
}
- (void)customReloadCellModel{
	[super customReloadCellModel];
    Menu *modelObject = self.cellModel.modelValue;
    if(modelObject.iconUrl.length||modelObject.iconName.length){
        self.iconImageView.image = modelObject.icon?:[UIImage imageNamed:@"placeholder.png"];
    }else{
        self.iconImageView.image = nil;
    }
    self.titleLabel.text = modelObject.title;
    self.selectButton.hidden = !self.cellModel.tableViewModel.isEditing;
    if(self.cellModel.tableViewModel.isEditing){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        self.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
}
- (CGSize)customSizeThatFits:(CGSize)size{//动态高度
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCellModel:(MKUITableViewCellModel *)cellModel{
//    NSLog(@"willDisplayCellModel:%@",cellModel.indexPathInModel);
    Menu *modelObject = cellModel.modelValue;
    if(!modelObject.icon&&modelObject.iconUrl.length!=0){//异步加载图片
        @MK_WEAKIFY(self);
        @MK_WEAKIFY(cellModel);
        [modelObject getIconWithCompletion:^(UIImage * _Nonnull image, NSError * _Nonnull error) {
            @MK_NORMALIZE(self);
            @MK_NORMALIZE(cellModel);
            if(image&&self.cellModel==cellModel){//异步完成之后，需要判断是否还是上一个单元格
//                NSArray *p1 = self.customCellModel.tableView.indexPathsForSelectedRows;
                [self.cellModel refreshWithAnimated:NO];
//                NSArray *p2 = self.customCellModel.tableView.indexPathsForSelectedRows;
            }
        }];
    }
}
@end
